#coding=utf-8

import unittest
import spider
from spider import TitleParser

class UrlParserTestor(unittest.TestCase):
    def setUp(self):
        self.parser = spider.UrlParser()      

class TitleParserTestor(unittest.TestCase):
    def setUp(self):
        self.parser = TitleParser()
        self.parser.setfilterrules({'data' : ['&','Python']})
              
    def test_handle_data(self):
        self.parser.handle_starttag('title', ())
        self.parser.handle_data("New in Python 3.4.2")
        self.parser.handle_starttag('title', ())
        self.parser.handle_data("Detail 3.4.2")
        
        self.assertEqual(len(self.parser.gettitles()), 1)
        self.assertEqual(self.parser.gettitles()[0], "New in Python 3.4.2")

class SpiderTestor(unittest.TestCase):       
    def test_visitedurls(self):
        s = spider.Spider()
        s.feed("/home/augustus/code/spider-test/index.html")
        self.assertEqual(len(s.getvisitedurls()), 9)
        
    def test_visitedurls_with_maxvisitedurls(self):
        s = spider.Spider()
        s.setmaxvisitedurls(5)
        s.feed("/home/augustus/code/spider-test/index.html")
        self.assertEqual(len(s.getvisitedurls()), 5)
    
    def test_titles(self):
        s = spider.TitleSpider()
        s.feed("/home/augustus/code/spider-test/index.html")
        self.assertEqual(len(s.gettitles()), 9)
    
    def test_titles_with_filter(self):    
        s = spider.TitleSpider()
        s.setfiltrules({'data' : ['&','Python']})
        s.feed("/home/augustus/code/spider-test/index.html")
        self.assertEqual(len(s.gettitles()), 3)
        
class FilterManagerTester(unittest.TestCase):
    def setUp(self):
        self.rules = {'prefix' : ['://', 'http', 'https'], 'postfix' : ['.', 'jpg', 'png'], 'data' : ['&','Python', 'new']}
        self.filtermanager = spider.FilterManager(self.rules)
        
    def test_updaterules(self):
        newrules = {'prefix' : ['://', 'ftp'], 'newfilter' : ['newfilterrule']}
        self.filtermanager.updaterules(newrules)

        self.assertEqual(self.filtermanager.getrules()['prefix'], ['://', 'ftp'])
        self.assertEqual(self.filtermanager.getrules()['postfix'], ['.', 'jpg', 'png'])
        self.assertEqual(self.filtermanager.getrules()['newfilter'], ['newfilterrule'])
        
        self.setUp()
    
    def test_matchprefix(self):
        self.assertTrue(self.filtermanager.matchprefix('prefix', 'http://baidu.com'))
        self.assertTrue(self.filtermanager.matchprefix('prefix', 'https://baidu.com'))
        self.assertFalse(self.filtermanager.matchprefix('prefix', 'ftp://baidu.com'))
        
        self.filtermanager.clearrules()
        self.assertTrue(self.filtermanager.matchprefix('prefix', 'ftp://baidu.com'))
        
        self.setUp()
        
    def test_matchpostfix(self):
        self.assertTrue(self.filtermanager.matchpostfix('postfix', 'http://baidu.jpg'))
        self.assertFalse(self.filtermanager.matchpostfix('postfix', 'http://sohu.txt'))
        
        self.filtermanager.clearrules()
        self.assertTrue(self.filtermanager.matchpostfix('postfix', 'http://sohu.txt'))
        
        self.setUp()
        
    def test_matchdata(self):
        self.assertTrue(self.filtermanager.matchdata('data', 'a new Python program.'))
        self.assertFalse(self.filtermanager.matchdata('data', 'a Python program.'))
        self.assertFalse(self.filtermanager.matchdata('data', 'a program.'))
        
        self.filtermanager.updaterules({'data' : ['||','Python', 'new']})
        self.assertTrue(self.filtermanager.matchdata('data', 'a new Python program.'))
        self.assertTrue(self.filtermanager.matchdata('data', 'a Python program.'))
        self.assertFalse(self.filtermanager.matchdata('data', 'a program.'))
        
def suite():
    suite = unittest.TestSuite()
    suite.addTest(FilterManagerTester("test_matchprefix"))
    return suite

if __name__ == '__main__':
    unittest.main() #defaultTest = 'suite'
